using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components
{
	public class KDTSelectHelper
	{
		public static SqlDataReader SelectReaderDynamic(string tableName, string whereCondition, string orderByExpression)
		{
			string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
			StringBuilder query = new StringBuilder();
			query.Append(string.Format("SELECT * FROM [dbo].[{0}]", tableName));
			if (whereCondition.Length > 0)
			{
				query.Append(" WHERE ");
				query.Append(whereCondition);
			}
			if (orderByExpression.Length > 0)
			{
				query.Append(" ORDER BY ");
				query.Append(orderByExpression);
			}
			
			return SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString());			
		}
		
		//-----------------------------------------------------------------------------------------
		public static DataSet SelectDataSetDynamic(string tableName, string whereCondition, string orderByExpression)
		{
			string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
			StringBuilder query = new StringBuilder();
			query.Append(string.Format("SELECT * FROM [dbo].[{0}]", tableName));
			if (whereCondition.Length > 0)
			{
				query.Append(" WHERE ");
				query.Append(whereCondition);
			}
			if (orderByExpression.Length > 0)
			{
				query.Append(" ORDER BY ");
				query.Append(orderByExpression);
			}
			
			return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query.ToString());			
		}

		//-----------------------------------------------------------------------------------------
		public static DataSet SelectDataSetDynamic(string selectQuery)
		{
			string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
			return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, selectQuery);
		}
	}
}
